package com.yhl;


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @ClassName ExcelWriterTest
 * @Description TODO
 * @Author yao
 * @Date 2024/2/22  15:00
 **/
public class ExcelWriterTest {

    String path = "C:\\Users\\Administrator\\Desktop\\Note\\smbms_-wx_-parent\\POI\\";


    @Test
    public void testWrite03() throws Exception {

        //1.创建一个工作簿SX
        Workbook workbook = new HSSFWorkbook();
        //2.创建工作表
        Sheet sheet = workbook.createSheet("张万森");
        //3.创建一个行
        Row row1 = sheet.createRow(0);

        //4.创建一个单元格第一行第一列
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("张万森");
        //(1,2)
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue("下雪了");

        Row row2 = sheet.createRow(1);
        //(2,1)
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("下雪时间");
        //(2,2)
        Cell cell22 = row2.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);

        FileOutputStream fileOutputStream = new FileOutputStream(path + "林北星.xls");

        workbook.write(fileOutputStream);

        fileOutputStream.close();
        System.out.println("林北星喜欢张万森");
    }

    @Test
    public void testWrite07() throws Exception {

        //1.创建一个工作簿
        Workbook workbook = new XSSFWorkbook();
        //2.创建工作表
        Sheet sheet = workbook.createSheet("林北星");
        //3.创建一个行
        Row row1 = sheet.createRow(0);

        //4.创建一个单元格第一行第一列
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("林北星");
        //(1,2)
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue("下雪了");

        Row row2 = sheet.createRow(1);
        //(2,1)
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("下雪时间");
        //(2,2)
        Cell cell22 = row2.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);

        FileOutputStream fileOutputStream = new FileOutputStream(path + "张万森.xlsx");

        workbook.write(fileOutputStream);

        fileOutputStream.close();
        System.out.println("张万森喜欢林北星");
    }


    @Test
    public void testWrite03BigData() throws Exception {
        long begin = System.currentTimeMillis();

        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        //写入数据
        for (int rowNum = 0; rowNum < 65535 ; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10 ; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue((rowNum+1)+"."+(cellNum+1));
            }
        }
        System.out.println("over");
        FileOutputStream fileOutputStream = new FileOutputStream(path + "testWriteBigData03.xls");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        long end = System.currentTimeMillis();
        System.out.println((double)(end-begin)/1000);
    }


    @Test
    public void testWrite07BigData() throws Exception {
        long begin = System.currentTimeMillis();

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        //写入数据
        for (int rowNum = 0; rowNum < 100000; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10 ; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue((rowNum+1)+"."+(cellNum+1));
            }
        }
        System.out.println("over");
        FileOutputStream fileOutputStream = new FileOutputStream(path + "testWriteBigData07.xlsx");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        long end = System.currentTimeMillis();
        System.out.println((double)(end-begin)/1000);
    }


    @Test
    public void testWrite07BigDataSpeed() throws Exception {
        long begin = System.currentTimeMillis();

        Workbook workbook = new SXSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        //写入数据
        for (int rowNum = 0; rowNum < 100000; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10 ; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue((rowNum+1)+"."+(cellNum+1));
            }
        }
        System.out.println("over");
        FileOutputStream fileOutputStream = new FileOutputStream(path + "testWriteBigData07.xlsx");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        //清除临时文件
        ((SXSSFWorkbook)workbook).dispose();
        long end = System.currentTimeMillis();
        System.out.println((double)(end-begin)/1000);
    }
}
